package userinterface;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.*;
import org.xml.sax.SAXException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.*;




/**
 * Servlet implementation class TokenServlet
 */
public class TokenServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TokenServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// Retrieve the token sent by Janrain 
		
        String token = request.getParameter("token");
        
        PrintWriter out = response.getWriter();
        //just print token on screen
        //out.println(token);
        
        String apiKey = "41bb51036ad1b159e0086088469e73f8b43e3e98";
        String baseUrl = "https://rpxnow.com/api/v2/auth_info?";
        String format = "xml";
        
        URL sendUrl = new URL(baseUrl + "apiKey=" + apiKey + "&token=" + token + "&format="+format);
        URLConnection conn = sendUrl.openConnection();
        
        try{
        
        	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        	dbf.setIgnoringElementContentWhitespace(true);
	        DocumentBuilder db = dbf.newDocumentBuilder();
	        Document doc = db.parse(conn.getInputStream());
	        
	        
	        Element root = doc.getDocumentElement();
	        NodeList rootList = root.getChildNodes();
        	
        	Node fstNode = rootList.item(0);
        	
        	if(fstNode.getNodeType() == Node.ELEMENT_NODE){
        		Element fstElement = (Element) fstNode;
        		
        		/* 
        		 * read display name
        		 */
        		NodeList ndlst = fstElement.getElementsByTagName("displayName");
        		Element displayNameElement = (Element)ndlst.item(0);
        		NodeList displayNameLst = displayNameElement.getChildNodes();
        		
        		out.println(displayNameLst.item(0).getNodeValue());
        		
        		
        		/*
        		 * read Email address
        		 */
        		ndlst = fstElement.getElementsByTagName("email");
        		Element emailElement = (Element)ndlst.item(0);
        		NodeList emailLst = emailElement.getChildNodes();
        		
        		out.println(emailLst.item(0).getNodeValue());
        		
        		/*
        		 * read Google Identifier
        		 */
        		ndlst = fstElement.getElementsByTagName("identifier");
        		Element idElement = (Element)ndlst.item(0);
        		NodeList identifier = idElement.getChildNodes();
        		
        		out.println(identifier.item(0).getNodeValue());
        		
        		/*
        		 * read givenName and familyName
        		 */
        		ndlst = fstElement.getElementsByTagName("name");
        		Element nameElement = (Element)ndlst.item(0);
        		NodeList nameLst = nameElement.getChildNodes();
        		
        		// given Name
        		Element givenNameElement = (Element)nameLst.item(0);
        		NodeList givenNameLst = givenNameElement.getChildNodes();
        		out.println(givenNameLst.item(0).getNodeValue());
        		
        		// family Name
        		Element familyName = (Element)nameLst.item(1);
        		NodeList familyNameLst = familyName.getChildNodes();
        		out.println(familyNameLst.item(0).getNodeValue());
        		
        		/*
        		 * Preferred Username
        		 */
        		ndlst = fstElement.getElementsByTagName("preferredUsername");
        		Element prefUsername = (Element)ndlst.item(0);
        		NodeList prefUsernameLst = prefUsername.getChildNodes();
        		
        		out.println(prefUsernameLst.item(0).getNodeValue());
        		
          		/*
        		 * Provider
        		 */
        		ndlst = fstElement.getElementsByTagName("providerName");
        		Element providerName = (Element)ndlst.item(0);
        		NodeList providerNameLst = providerName.getChildNodes();
        		
        		out.println(providerNameLst.item(0).getNodeValue());
        		
        		
        		/*
        		 * Url
        		 */
        		ndlst = fstElement.getElementsByTagName("url");
        		Element url = (Element)ndlst.item(0);
        		NodeList urlLst = url.getChildNodes();
        		
        		out.println(urlLst.item(0).getNodeValue());
        		
        		/*
        		 * Google UserId
        		 */
        		ndlst = fstElement.getElementsByTagName("googleUserId");
        		Element userId = (Element)ndlst.item(0);
        		NodeList userIdLst = userId.getChildNodes();
        		
        		out.println(userIdLst.item(0).getNodeValue());
        		
        		
        	}
       
        
        } catch (Exception e){
        	e.printStackTrace();
        }

        
       
        
	}

}
